﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_RobotQuotation : UserControl
    {
        public panel_RobotQuotation()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.RobotQuotation.PopupMenuShowing += RobotQuotation_PopupMenuShowing;
            this.robotQuotationContextMenu.ItemClicked += robotQuotationContextMenu_ItemClicked;
            this.RobotQuotation.DoubleClick += RobotQuotation_DoubleClick;
        }

        //////////////////////////////////////////
        /// Call Edit Form
        ///////////////////////////////////////////
        private void callEditForm()
        {
            
            DataRow row = RobotQuotation.GetFocusedDataRow();
            frm_Edit_RobotQuotation edit_form = new frm_Edit_RobotQuotation();
            edit_form.id = row["ID"].ToString();
            if (edit_form.LoadData())
            {
                edit_form.ShowDialog();
                LoadData();
            }
            else
            {
                edit_form.Dispose();
                MessageBox.Show("Cannot get data from Quotation list");
            } 
        }

        void RobotQuotation_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }

        void robotQuotationContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.robotQuotationContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "changeSelection":
                    callEditForm();
                    LoadData();
                    break;
                case "deleteSelection":
                    deleteData();
                    LoadData();
                    break;
                case "addnewSelection":
                    frm_Add_RobotQuotation add_form = new frm_Add_RobotQuotation();
                    add_form.ShowDialog();
                    LoadData();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void RobotQuotation_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.robotQuotationContextMenu.Show(MousePosition);            
        }       

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_RobotQuotation add_form = new frm_Add_RobotQuotation();
            add_form.ShowDialog();
            LoadData();
        }

        
        /////////////////////////////////////////
        /// Load Export Robot Plan into gridview
        /////////////////////////////////////////
        private void LoadData()
        {            
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT t1.ID, t1.QuotationID, t1.CompID, t5.SupplierName, t5.PIC,"
                + " t1.Quantity1, t1.Quantity2, t1.Quantity3, t1.Quantity4, t1.Quantity5,"
                + " t1.Price1, t1.Price2, t1.Price3, t1.Price4, t1.Price5, t1.CurrencyID, t1.ExchangeRate,"
                + " t2.DeliveryTermName, t1.Note, t3.InputDate, t3.ExpiredDate"
                + " FROM [241_RobotPrice] AS t1 LEFT OUTER JOIN"
                + " [222_DeliveryTerm] AS t2 ON t1.PriceType=t2.DeliveryTermID LEFT OUTER JOIN"
                + " [241_ExportQuotation] AS t3 ON t1.QuotationID=t3.QuotationID LEFT JOIN"
                //+ " [222_Part] AS t4 ON t1.CompID=t4.PartNo LEFT JOIN"
                + " [222_Supplier] AS t5 ON t1.SupplierID=t5.SupplierID"
                + " ORDER BY t1.QuotationID DESC, t1.CompID";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridRobotQuotation.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Robot Quotation");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("QuotationID", typeof(string));
                newTable.Columns.Add("Comp ID", typeof(string));
                newTable.Columns.Add("Supplier", typeof(string));
                newTable.Columns.Add("Contact", typeof(string));
                newTable.Columns.Add("Quantity1", typeof(int));
                newTable.Columns.Add("Quantity2", typeof(int));
                newTable.Columns.Add("Quantity3", typeof(int));
                newTable.Columns.Add("Quantity4", typeof(int));
                newTable.Columns.Add("Quantity5", typeof(int));
                newTable.Columns.Add("Price1", typeof(float));
                newTable.Columns.Add("Price2", typeof(float));
                newTable.Columns.Add("Price3", typeof(float));
                newTable.Columns.Add("Price4", typeof(float));
                newTable.Columns.Add("Price5", typeof(float));
                newTable.Columns.Add("Currency", typeof(string));
                newTable.Columns.Add("ExchangeRate", typeof(float));
                newTable.Columns.Add("PriceType", typeof(int));
                newTable.Columns.Add("Note", typeof(string));
                newTable.Columns.Add("InputDate", typeof(DateTime));
                newTable.Columns.Add("ExpiredDate", typeof(DateTime));
                gridRobotQuotation.DataSource = newTable;                
            }
            RobotQuotation.Columns[0].Caption = "ID";
            RobotQuotation.Columns[1].Caption = "Quotation ID";
            RobotQuotation.Columns[2].Caption = "Robot Name";
            RobotQuotation.Columns[3].Caption = "Supplier";
            RobotQuotation.Columns[4].Caption = "Contact";
            RobotQuotation.Columns[5].Caption = "Quantity 1";
            RobotQuotation.Columns[6].Caption = "Quantity 2";
            RobotQuotation.Columns[7].Caption = "Quantity 3";
            RobotQuotation.Columns[8].Caption = "Quantity 4";
            RobotQuotation.Columns[9].Caption = "Quantity 5";
            RobotQuotation.Columns[10].Caption = "Price 1";
            RobotQuotation.Columns[11].Caption = "Price 2";
            RobotQuotation.Columns[12].Caption = "Price 3";
            RobotQuotation.Columns[13].Caption = "Price 4";
            RobotQuotation.Columns[14].Caption = "Price 5";
            RobotQuotation.Columns[15].Caption = "Currency";
            RobotQuotation.Columns[16].Caption = "Exchange Rate";
            RobotQuotation.Columns[17].Caption = "Price Type";
            RobotQuotation.Columns[18].Caption = "Note";
            RobotQuotation.Columns[19].Caption = "Input Date";
            RobotQuotation.Columns[20].Caption = "Expired Date";
            RobotQuotation.BestFitColumns();
            RobotQuotation.Columns[0].Visible = false;
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void deleteData()
        {
            if (MessageBox.Show("Are you really to delete selected record now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //focusIndex = CustomClerance.GetFocusedDataSourceRowIndex();
                int[] selected_row = RobotQuotation.GetSelectedRows();
                myConnection.Open();
                int k = 0;
                foreach (int i in selected_row)
                {
                    bool exist = false;
                    try
                    {
                        SqlCommand cmd_sql = myConnection.CreateCommand();
                        cmd_sql.CommandText = "SELECT * FROM [241_RobotPrice] WHERE"
                            + " ID='" + RobotQuotation.GetRowCellValue(i, "ID").ToString() + "'";
                        SqlDataReader dataReader = cmd_sql.ExecuteReader();
                        if (dataReader.Read())
                        {
                            exist = true;
                        }
                        dataReader.Close();
                        cmd_sql.Dispose();
                    }
                    catch
                    {
                        exist = false;
                    }
                    if (exist)
                    {
                        try
                        {
                            string sql = "DELETE FROM [241_RobotPrice] "
                                            + "WHERE ID='" + RobotQuotation.GetRowCellValue(i, "ID").ToString() + "'";
                            SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                            cmd_sql.ExecuteNonQuery();
                            cmd_sql.Dispose();
                            k++;
                        }
                        catch
                        {

                        }
                    }
                }
                myConnection.Close();
                MessageBox.Show(k + " recoreds have been deleted.");
                LoadData();
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteData();
        }
    }
}
